#ifndef __AS_GRAPHIC__H__
#define __AS_GRAPHIC__H__

#include "ASStdLib.h"
//#include "..\tool\ASLog.h"


/*FOR MRE*/
#define  RGB_TO_BGR(color)          (((color & 0xff0000) >> 16) + (color & 0x00ff00) + ((color & 0xff) << 16))
#define AS_COLOR_BGR_TO_565(i)	    ((i & 0xf8) << 8) + ((i & 0xfc00) >> 5) + ((i & 0xf80000) >> 19)
#define AS_COLOR_RGB_TO_565(c)   AS_COLOR_BGR_TO_565(RGB_TO_BGR(c))

/*FOR SKY*/
#define AS_COLOR_GET_R(c) (ASUINT8)((c & 0xff0000) >> 16)
#define AS_COLOR_GET_G(c) (ASUINT8)((c & 0x00ff00) >> 8)
#define AS_COLOR_GET_B(c) (ASUINT8)((c & 0xff))
#define AS_COLOR_GEN_RGB(r,g,b) ((r << 16) | (g << 8) | (b))
#define AS_COLOR_GEN_RGB565(r,g,b) (ASUINT16) ( ((ASUINT)(r>>3) << 11) + ((ASUINT)(g>>2) << 5) + ((ASUINT)(b>>3)) )

/*colors*/
#define AS_COLOR_TRANS   0x110000
#define AS_GRID_COLOR1   0xa7aaaa
#define AS_GRID_COLOR2   0xfdffff

#define AS_COLOR_WHITE   0xffffff
#define AS_COLOR_BLACK   0x0
#define AS_COLOR_RED     0xff0000
#define AS_COLOR_GREEN   0x00ff00
#define AS_COLOR_BLUE    0x0000ff
#define AS_COLOR_GREEN_GRAM 0xbbfeb9
#define AS_COLOR_GRAY_1  0xeeeeee
#define AS_COLOR_GRAY_2  0xdddddd

/*intersection of two rectangles*/
#define AS_RECT_INTER_X(r1,r2)  (AS_MAX((r1).x, (r2).x))
#define AS_RECT_INTER_Y(r1,r2)  (AS_MAX((r1).y, (r2).y))
#define AS_RECT_INTER_W(r1,r2)  (AS_MIN((r1).x+(r1).w,(r2).x+(r2).w) - AS_RECT_INTER_X(r1,r2))
#define AS_RECT_INTER_H(r1,r2)  (AS_MIN((r1).y+(r1).h,(r2).y+(r2).h) - AS_RECT_INTER_Y(r1,r2))

typedef enum
{
    AS_INPUT_NORMAL,
    AS_INPUT_NUMBER,
    AS_INPUT_PASSWORD
} as_input_type;

typedef struct
{
    ASINT res_hdlr;
    ASINT res_w;
    ASINT res_h;
} as_res_info;


ASUINT16 as_get_lcd_width(ASVOID);
ASUINT16 as_get_lcd_height(ASVOID);

ASVOID as_flush(ASVOID);
ASVOID as_disable_flush(ASVOID);
ASVOID as_enable_flush(ASVOID);
ASBOOL as_get_if_disable_flush(ASVOID);
ASVOID as_reset_enable_flush(ASVOID);

as_ret as_set_layer_clip_ex(ASINT layer, const as_rect* area_p);
ASVOID as_get_global_clip(as_rect* clip_p);
ASVOID as_reset_layer_clip(ASINT layer);

#if defined(AS_RUN_ON_SKY)
ASVOID as_reset_clip(ASVOID);
#endif

ASINT as_get_layer_hdlr_by_buffer(ASUINT8* buffer);
ASUINT8* as_get_layer_buffer_by_hdlr(ASINT hdlr);
ASVOID as_draw_h_line(ASINT16 x1, ASINT16 x2, ASINT16 y, ASUINT32 line_color);
ASVOID as_draw_v_line(ASINT16 x, ASINT16 y1, ASINT16 y2, ASUINT32 line_color);
ASVOID as_fill_rect(const as_rect* area_p, ASUINT32 line_color, ASUINT32 fill_color);
ASVOID as_draw_rect(const as_rect* area_p, ASUINT32 line_color);
ASVOID as_fill_circle(ASINT x, ASINT y, ASINT r, ASUINT color);
ASVOID as_draw_pixel(ASINT x, ASINT y, ASUINT color);
ASVOID as_draw_line(ASINT16 x1, ASINT16 y1, ASINT16 x2, ASINT16 y2, ASUINT32 line_color);

ASINT as_get_wstr_width(ASWSTR wstr);
ASINT as_get_wstr_height(ASWSTR wstr);
ASVOID as_draw_text(ASWSTR text, ASINT x, ASINT y, ASUINT color);
ASVOID as_draw_text_clip(ASWSTR text, ASINT x, ASINT y, as_rect* clip, ASUINT color);

#ifdef AS_SMALL_FONT_ENABLE
ASVOID as_small_font_load(void);
ASVOID as_small_font_unload(void);
ASINT as_get_wstr_width_sf(ASWSTR wstr);
ASINT as_get_wstr_height_sf(ASWSTR wstr);
ASVOID as_draw_text_sf(ASWSTR text, ASINT x, ASINT y, ASUINT color);
ASVOID as_draw_text_clip_sf(ASWSTR text, ASINT x, ASINT y, as_rect* clip, ASUINT color);
#else
#define as_get_wstr_width_sf as_get_wstr_width
#define as_get_wstr_height_sf as_get_wstr_height
#define as_draw_text_sf as_draw_text
#define as_draw_text_clip_sf as_draw_text_clip
#endif//AS_SMALL_FONT_ENABLE

ASVOID as_screen_clear(ASVOID);

/*edit screen*/
ASVOID as_create_input_screen(const ASWCHAR* title, const ASWCHAR* default_str,
                              as_input_type type, ASUINT max_len, ASWCHAR* result, as_void_func ok_cb);
ASVOID as_destroy_input_screen(ASBOOL if_save_text);

/*internal resource*/
ASINT as_load_res_internal(const ASSTR res_name, as_res_info* res_info);
ASVOID as_unload_res_internal(const as_res_info* res_info);
ASVOID as_draw_image(ASUINT8* buf, ASINT16 x, ASINT16 y, const as_res_info* res_info);
ASVOID as_draw_image_trans(ASUINT8* buf, ASINT16 x, ASINT16 y, const as_res_info* res_info);


#endif//__AS_GRAPHIC__H__